package pe.edu.upc.dew.grupo5.web;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo5.model.Boleto;
import pe.edu.upc.dew.grupo5.model.Cliente;
import pe.edu.upc.dew.grupo5.service.BoletoService;
import pe.edu.upc.dew.grupo5.service.ClienteService;
import pe.edu.upc.dew.grupo5.service.FuncionService;

/**
 * Servlet implementation class VentaEntradaServlet
 */

public class VentaEntradaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BoletoService boletoService;
	private ClienteService clienteService;
	private FuncionService funcionService;

	public void init(ServletConfig config) throws ServletException {
		this.boletoService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"boletoService", BoletoService.class);
		this.clienteService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"clienteService", ClienteService.class);
		this.funcionService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"funcionService", FuncionService.class);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Cliente cliente = new Cliente();
		String email = (String) session.getAttribute("usuario");
		int idFuncion = Integer.valueOf(request.getParameter("idFuncion"));
		int idCine = Integer.valueOf(request.getParameter("idCine"));
		int cantidad = Integer.valueOf(request.getParameter("cantidad"));

		if (email != null){
			cliente = clienteService.getClientePorMail(email);
			request.setAttribute("cliente", cliente);
		}
		try {
			int idCliente =  clienteService.getClientePorMail(email).getIdCliente();

			if (cantidad > funcionService.entradasDisponibles(idFuncion)){
				request.setAttribute("error", "Cantidad supera el total de entradas disponible.");
			}else{
				Boleto boleto = boletoService.insertaBoleto(idFuncion,cantidad,idCliente);
				request.setAttribute("boleto", boleto);
			}
			request.getRequestDispatcher("compraonline.jsp").forward(request,
					response);
		} catch (Exception e) {
			System.out.println("error: " + e);
			request.setAttribute("error", e);
			request.getRequestDispatcher("compraonline.jsp").forward(request,
					response);
		}
	}

}
